/**
 * @author dujiabao
 * @description 模拟实现基于文本界面的《家庭记账软件》。该软件能够记录家庭的收入、支出，并能够打印收支明细表。
 */

package familyAccount;

import utility.Utility;


public class FamilyAccount {
    private static String[] illustrate = new String[100]; // 说明
    private static int[] account = new int[100];  // 当前金额
    private static int count = 0;  // 记录的个数

    /**
     * main 函数
     *
     * @param args 调用接口
     */
    public static void main(String[] args) {
        //初始化
        account[0] = 10000;
        illustrate[0] = "生活基本金";
        count++;
        label:
        //留下标签，以供后面break
        while (true) {
            System.out.println("-----------------家庭收支记账软件-----------------");
            System.out.println("                   1 收支明细");
            System.out.println("                   2 登记收入");
            System.out.println("                   3 登记输出");
            System.out.println("                   4 退出");
            System.out.println();
            System.out.println("                 请选择(1-4):");

            char selection = Utility.readMenuSelection();  //输入选择
            switch (selection) {
                case '1' -> {
                    System.out.println("-----------------当前收支明细记录-----------------");
                    System.out.printf("%-6s%-12s%-12s%-12s\n", "收支", "账户金额", "收支金额", "说明");
                    for (int i = 1; i < count; i++) {
                        int change = account[i] - account[i - 1];
                        if (change >= 0) {
//                            System.out.println("收入\t\t" + account[i] + "\t\t" + change + "\t\t" + illustrate[i]);
                            System.out.printf("%-6s%-12d%-12d%-12s\n", "收入", account[i], change, illustrate[i]);
                        } else {
//                            System.out.println("支出\t\t" + account[i] + "\t\t" + -change + "\t\t" + illustrate[i]);
                            System.out.printf("%-6s%-12d%-12d%-12s\n", "支出", account[i], -change, illustrate[i]);
                        }
                    }
                    System.out.println();
                }
                case '2' -> {
                    System.out.print("本次收入金额：");
                    account[count] = account[count - 1] + Utility.readNumber();  //重新计算当前金额
                    System.out.print("本次收入说明：");
                    illustrate[count] = Utility.readString();  //记录说明
                    count++;
                }
                case '3' -> {
                    System.out.print("本次支出金额：");
                    account[count] = account[count - 1] - Utility.readNumber();
                    System.out.print("本次支出说明：");
                    illustrate[count] = Utility.readString();
                    count++;
                }
                case '4' -> {
                    System.out.print("请确认是否退出！(Y/N)");  //确认退出
                    if (Utility.readConfirmSelection() == 'Y') {
                        break label;
                    }
                }
            }
        }
    }
}